package com.flyme.module.base.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysCollecon;
import com.flyme.module.base.provider.mapper.SysColleconMapper;
import com.flyme.module.base.provider.service.SysColleconService;
import org.springframework.stereotype.Service;


@Service
public class SysColleconServiceImpl extends BaseServiceImpl<SysColleconMapper, SysCollecon> implements SysColleconService {

    /**
     * 查询用户是否已经收藏/点赞/关注
     *
     * @return
     */
    public Integer isCollecon(Long targetId, Long userId, Integer targetType, Integer optType) {
        QueryWrapper queryWrapper = new QueryWrapper<SysCollecon>();
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("targetType", targetType);
        queryWrapper.eq("optType", optType);
        return count(queryWrapper);
    }

    /**
     * 查找对象
     */
    public SysCollecon findCollecon(Long targetId, Long userId, Integer targetType, Integer optType) {
        QueryWrapper queryWrapper = new QueryWrapper<SysCollecon>();
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("targetType", targetType);
        queryWrapper.eq("optType", optType);
        return getOne(queryWrapper);
    }

    /**
     * 根据目标统计点赞/收藏数量
     */
    public Integer countByTarget(String targetId, Integer type) {
        return count(q().eq("targetId", targetId).eq("optType", type));
    }

    /**
     * 网站根据用户查询收藏/点赞
     */
    public PqGrid selectByUserWeb(Long accountInfoId, Integer optType, PagerInfo pagerInfo){
        pagerInfo.setPageNumber(8);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("c.targetType,c.id colleconId,c.targetId,cs.isVipFree,CASE WHEN ISNULL(cs.coursePrice) THEN 0 ELSE cs.coursePrice END coursePrice,"
                + "CASE WHEN ISNULL(cs.onLine) THEN 2 ELSE cs.onLine END onLine,"
                + "CASE WHEN ISNULL(cs.courseImage) THEN live.liveImage ELSE cs.courseImage END img,"
                + "CASE WHEN ISNULL(cs.courseTitle) THEN live.liveTitle ELSE cs.courseTitle END title,"
                + "CASE WHEN ISNULL(cs.courseCount) THEN 0 ELSE cs.courseCount END cataNum,"
                + "CASE WHEN ISNULL(l.lecturerName) THEN le.lectu"
                + "rerName ELSE l.lecturerName END lecturerName");
        queryWrapper.eq("userId", accountInfoId);
        queryWrapper.eq("optType", optType);
        queryWrapper.orderByDesc("c.createTime");
        queryWrapper.createAlias("course", "cs");
        queryWrapper.createAlias("lecturer", "l");
        queryWrapper.createAlias("live", "live");
        queryWrapper.createAlias("lecturer2", "le");
        PqGrid pageList = pageList(queryWrapper);
        return pageList(queryWrapper);
    }
}
